package com.usooft.metaTalent.core.system.service.base;

import com.usooft.framework.core.query.Limit;
import com.usooft.framework.core.query.Page;
import com.usooft.metaTalent.core.system.entity.DictItem;
import com.usooft.metaTalent.core.system.mapper.DictItemMapper;
import com.usooft.metaTalent.core.system.query.DictItemQuery;
import com.usooft.metaTalent.core.system.service.DictItemService;
import com.usooft.metaTalent.core.system.wrapper.DictItemWrapper;
import java.lang.Long;
import java.lang.Override;
import java.lang.String;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.lang.NonNull;

public abstract class BaseDictItemService implements DictItemService {
    @Autowired
    private DictItemMapper dictItemMapper;

    @Override
    public DictItem get(@NonNull Long id) {
        return find(id).orElseThrow();
    }

    @Override
    public Optional<DictItem> find(@NonNull Long id) {
        return Optional.ofNullable(dictItemMapper.get(id));
    }

    @Override
    public List<DictItemWrapper> list(DictItemQuery query) {
        return dictItemMapper.list(query);
    }

    @Override
    public Page<DictItemWrapper> page(DictItemQuery query) {
        int rows = dictItemMapper.count(query.getFilter());
        return new Page<>(rows, rows == 0 ? new ArrayList<>() : dictItemMapper.list(query));
    }

    protected Optional<DictItem> findOne(DictItemQuery.Filter filter, DictItemQuery.Sorts sorts) {
        List<DictItemWrapper> wrappers = list(DictItemQuery.builder()
                                .filter(filter)
                                .sorts(sorts)
                                .limit(Limit.rowCount(1))
                                .build());
        return wrappers.stream().findFirst().map(Function.identity());
    }

    protected Optional<DictItem> findOne(DictItemQuery.Filter filter) {
        return findOne(filter, DictItemQuery.Sorts.builder().build());
    }

    @Override
    public int count(DictItemQuery.Filter filter) {
        return dictItemMapper.count(filter);
    }

    protected DictItem internalAdd(Long dictId, String value) {
        DictItem dictItem = new DictItem();
        dictItem.setDictId(dictId);
        dictItem.setValue(value);

        dictItem.setCreatedDate(LocalDateTime.now());
        dictItem.setVersion(0);
        dictItemMapper.add(dictItem);
        return dictItem;
    }

    protected void internalUpdate(@NonNull DictItem dictItem) {
        dictItem.setLastModifiedDate(LocalDateTime.now());

        int rows = dictItemMapper.update(dictItem);
        if (rows == 0) {
            throw new OptimisticLockingFailureException("更新失败。可能字典项 (" + dictItem.getId() + ") 的版本 (" + dictItem.getVersion() + ") 已不是最新");
        }
        dictItem.setVersion(dictItem.getVersion() + 1);
    }

    protected void internalDelete(@NonNull DictItem dictItem) {
        int rows = dictItemMapper.delete(dictItem);
        if (rows == 0) {
            throw new OptimisticLockingFailureException("删除失败。可能字典项 (" + dictItem.getId() + ") 的版本 (" + dictItem.getVersion() + ") 已不是最新");
        }
    }
}
